package org.jeecg.modules.veterans.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.util.SecurityUtil;
import org.jeecg.modules.veterans.entity.SysActivities;
import org.jeecg.modules.veterans.entity.SysService;
import org.jeecg.modules.veterans.entity.SysServiceApp;
import org.jeecg.modules.veterans.service.ISysActivitiesService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.veterans.service.ISysServiceAppService;
import org.jeecg.modules.veterans.service.ISysServiceService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 活动集结信息表
 * @Author: jeecg-boot
 * @Date:   2022-05-07
 * @Version: V1.0
 */
@Api(tags="活动集结信息表")
@RestController
@RequestMapping("/veterans/sysActivities")
@Slf4j
public class SysActivitiesController extends JeecgController<SysActivities, ISysActivitiesService> {
	@Autowired
	private ISysActivitiesService sysActivitiesService;
	@Autowired
	private ISysServiceAppService sysServiceService;

	
	/**
	 * 分页列表查询
	 *
	 * @param sysActivities
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "活动集结信息表-分页列表查询")
	@ApiOperation(value="活动集结信息表-分页列表查询", notes="活动集结信息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<SysActivities>> queryPageList(SysActivities sysActivities,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		sysActivities.setStatus("1");
		QueryWrapper<SysActivities> queryWrapper = QueryGenerator.initQueryWrapper(sysActivities, req.getParameterMap());
		LoginUser loginUser = SecurityUtil.getLoginUser();
		String dep = loginUser.getOrgCode();
		if(StringUtils.isNotBlank(dep)){
			switch (dep){
				case "A02A02":
					queryWrapper.eq("area","1");
					break;
				case "A02A01":
					queryWrapper.eq("area","2");
					break;
				case "A02A03":
					queryWrapper.eq("area","3");
					break;
				case "A02A04":
					queryWrapper.eq("area","4");
					break;
				case "A02A05":
					queryWrapper.eq("area","5");
					break;
				case "A02A07":
					queryWrapper.eq("area","6");
					break;
				case "A02A08":
					queryWrapper.eq("area","7");
					break;
				case "A02A09":
					queryWrapper.eq("area","8");
					break;
				case "A02A10":
					queryWrapper.eq("area","9");
					break;
			}
		}
		Page<SysActivities> page = new Page<SysActivities>(pageNo, pageSize);
		IPage<SysActivities> pageList = sysActivitiesService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 分页列表查询
	  *
	  * @param sysActivities
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "活动集结信息表-分页列表查询")
	 @ApiOperation(value="活动集结信息表-分页列表查询", notes="活动集结信息表-分页列表查询")
	 @GetMapping(value = "/listSh")
	 public Result<IPage<SysActivities>> queryPageListSh(SysActivities sysActivities,
													   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													   HttpServletRequest req) {
	 	 sysActivities.setStatus("2");
		 QueryWrapper<SysActivities> queryWrapper = QueryGenerator.initQueryWrapper(sysActivities, req.getParameterMap());
		 LoginUser loginUser = SecurityUtil.getLoginUser();
		 String dep = loginUser.getOrgCode();
		 switch (dep){
			 case "A02A02":
				 queryWrapper.eq("area","1");
				 break;
			 case "A02A01":
				 queryWrapper.eq("area","2");
				 break;
			 case "A02A03":
				 queryWrapper.eq("area","3");
				 break;
			 case "A02A04":
				 queryWrapper.eq("area","4");
				 break;
			 case "A02A05":
				 queryWrapper.eq("area","5");
				 break;
			 case "A02A07":
				 queryWrapper.eq("area","6");
				 break;
			 case "A02A08":
				 queryWrapper.eq("area","7");
				 break;
			 case "A02A09":
				 queryWrapper.eq("area","8");
				 break;
			 case "A02A10":
				 queryWrapper.eq("area","9");
				 break;
		 }
		 Page<SysActivities> page = new Page<SysActivities>(pageNo, pageSize);
		 IPage<SysActivities> pageList = sysActivitiesService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	
	/**
	 *   添加
	 *
	 * @param sysActivities
	 * @return
	 */
	@AutoLog(value = "活动集结信息表-添加")
	@ApiOperation(value="活动集结信息表-添加", notes="活动集结信息表-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody SysActivities sysActivities) {
		sysActivities.setState("1");
		sysActivities.setStatus("2");
		sysActivitiesService.save(sysActivities);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param sysActivities
	 * @return
	 */
	@AutoLog(value = "活动集结信息表-编辑")
	@ApiOperation(value="活动集结信息表-编辑", notes="活动集结信息表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody SysActivities sysActivities) {
		sysActivitiesService.updateById(sysActivities);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "活动集结信息表-通过id删除")
	@ApiOperation(value="活动集结信息表-通过id删除", notes="活动集结信息表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		sysActivitiesService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "活动集结信息表-批量删除")
	@ApiOperation(value="活动集结信息表-批量删除", notes="活动集结信息表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysActivitiesService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "活动集结信息表-通过id查询")
	@ApiOperation(value="活动集结信息表-通过id查询", notes="活动集结信息表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<SysActivities> queryById(@RequestParam(name="id",required=true) String id) {
		SysActivities sysActivities = sysActivitiesService.getById(id);
		if(sysActivities==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(sysActivities);
	}

	 /**
	  * 通过phone查询
	  *
	  * @param phone
	  * @return
	  */
	 //@AutoLog(value = "申请审批结果查询-通过phone查询")
	 @ApiOperation(value="申请审批结果查询-通过phone查询", notes="申请审批结果查询-通过phone查询")
	 @GetMapping(value = "/search")
	 public Result<Map> search(@RequestParam(name="phone",required=true) String phone) {
		 QueryWrapper<SysActivities> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("phone",phone);
		 List<SysActivities> sysActivities = sysActivitiesService.selectList(queryWrapper);
		 String activity = "";
		 for (SysActivities item:sysActivities) {
			 activity = item.getHdmc() + ": "+ (item.getState()=="1"?"通过":"未通过") + "\n";
		 }

		 QueryWrapper<SysServiceApp> queryWrapper1 = new QueryWrapper<>();
		 queryWrapper1.eq("phone",phone);
		 List<SysServiceApp> sysServices = sysServiceService.selectList(queryWrapper1);
		 String service = "";
		 for (SysServiceApp item:sysServices) {
			 service = service + item.getName() + ": "+ (item.getState()=="1"?"通过":"未通过") + "\n";
		 }

		 Map<String,String> map = new HashMap<>();
		 map.put("activity",activity);
		 map.put("service",service);

		 return Result.ok(map);
	 }

	 /**
	  *   现场核验
	  *
	  * @param sysActivities
	  * @return
	  */
	 @AutoLog(value = "活动集结信息表-现场核验")
	 @ApiOperation(value="活动集结信息表-现场核验", notes="活动集结信息表-现场核验")
	 @PostMapping(value = "/check")
	 public Result<String> check(@RequestBody SysActivities sysActivities) {
		 sysActivities.setChecker(SecurityUtil.getLoginUser().getRealname());
		 sysActivities.setCheckTime(new Date());
		 sysActivitiesService.updateById(sysActivities);
		 return Result.OK("核验成功！");
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param sysActivities
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysActivities sysActivities) {
        return super.exportXls(request, sysActivities, SysActivities.class, "活动集结信息表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysActivities.class);
    }

}
